﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class BorrowDAL
    {
        SqlCommand cmd;//定义Command对象
        DataSet ds;//定义数据集对象

        static string str = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
        static SqlConnection conn = new SqlConnection(str);

        public BorrowDAL()
        {
            cmd = new SqlCommand();
            cmd.CommandType = CommandType.StoredProcedure;//指定Command对象执行方式为存储过程
            ds = new DataSet();//实例化数据集对象
        }

        public MemoryStream GetStuPic(Guid stuID)
        {
            cmd.CommandText = "GetUserPhotoByID";//定义调用的存储过程名称
            cmd.Parameters.Clear();//清除参数
            cmd.Parameters.Add("@id", SqlDbType.UniqueIdentifier).Value = stuID;//加载参数并完成参数传值
            try
            {
                MemoryStream obj = DBHelper.GetPhoto(cmd);
                return obj;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
        public DataSet GetUserInfoByStuNo(string stuno)
        {
            cmd.CommandText = "GetUserInfoByStuNo";//定义调用的存储过程名称
            cmd.Parameters.Clear();//清除参数
            cmd.Parameters.Add("@stuno", SqlDbType.Char, 11).Value = stuno;//加载参数并完成参数传值
            try
            {
                ds= DBHelper.QueryData(cmd);//调用通用数据访问类获取数据库数据
                return ds;//返回数据集
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 查看书籍状态，判断是否可借
        /// </summary>
        /// <param name="bookid">索书号</param>
        /// <returns>true：可借；false：不可借</returns>
        public bool GetBookState(string bookid)
        {
            try
            {
                cmd.CommandText = "GetBookState";//定义调用的存储过程名称
                cmd.Parameters.Clear();//清除参数
                cmd.Parameters.Add("@bookid", SqlDbType.VarChar, 20).Value = bookid;//加载参数并完成参数传值
                cmd.Parameters.Add("@returnValue", SqlDbType.Bit).Direction = ParameterDirection.Output;//存储过程回传值的传参
                DBHelper.ExecuteSQL(cmd);

                var result = cmd.Parameters["@returnValue"].Value;
                return bool.Parse(result.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
